import { ShopState } from '../../interface/index.d';

const initState: ShopState = {
    list: [
        {
            id: 1,
            name: '纯牛奶 100ml',
            quantity: 2,
            unit_price: 5,
            checked: false
        }
    ]
};

export const shopReducer = (state = initState, action: any) => {
    const newState: ShopState = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case 'ADD_GOOD_ITEM':
            newState.list.forEach(element => {
                if (element.id === action.id) {
                    element.quantity++;
                }
            });
            return newState;
        case 'SUB_GOOD_ITEM':
            newState.list.forEach(element => {
                if (element.id === action.id) {
                    element.quantity--;
                }
            });
            return newState;
        case 'CHECK_GOOD_ITEM':
            newState.list.forEach(element => {
                if (element.id === action.item.id) {
                    element.checked = !element.checked;
                }
            });
            return newState;
        default:
            return state;
    }
}